{template 'common/header-base'}
<style type="text/css">
    .we7-form input[type=radio]+label, .we7-form input[type=checkbox]+label {
        cursor: pointer;
        display: inline-block;
        position: relative;
        padding-left: 25px;
        margin-right: 90px;
        color: #252424;
        font-weight: 400;
    }
</style>
{if $op == 'display'}
<form action="" method="post" class="we7-form form" id="">
    <div class="form-group" style="color: red;font-size: 18px;text-align: right;">开启微擎全局远程附件后，模块内的全局设置要关闭。二选一！</div>
    <div class="form-group">
        <div class="col-sm-12">
            <input type="radio" name="type" id="type-0" value="0" onclick="$('.remote-qiniu').hide();$('.remote-alioss').hide();$('.remote-ftp').hide();$('.remote-close').show();$('.remote-cos').hide();" {if empty($remote['type']) || $remote['type'] == '0'} checked="checked" {/if}>
            <label class="radio-inline" for="type-0">
                 关闭
            </label>
            <input type="radio" name="type" id="type-1" value="1" onclick="$('.remote-qiniu').hide();$('.remote-ftp').show();$('.remote-alioss').hide();$('.remote-close').hide();$('.remote-cos').hide();" {if !empty($remote['type']) && $remote['type'] == '1'} checked="checked" {/if}>
            <input type="radio" name="type" id="type-2" value="2" onclick="$('.remote-qiniu').hide();$('.remote-alioss').show();$('.remote-ftp').hide();$('.remote-close').hide();$('.remote-cos').hide();" {if !empty($remote['type']) && $remote['type'] == '2'} checked="checked" {/if}>
            <label class="radio-inline" for="type-2">
                 阿里云OSS <span class="label label-success">推荐，快速稳定</span>
            </label>
            <input type="radio" name="type" id="type-3" value="3" onclick="$('.remote-qiniu').show();$('.remote-alioss').hide();$('.remote-ftp').hide();$('.remote-close').hide();$('.remote-cos').hide();" {if !empty($remote['type']) && $remote['type'] == '3'} checked="checked" {/if}>
            <label class="radio-inline" for="type-3">
                 七牛云存储 <span class="label label-success">推荐，快速稳定</span>
            </label>
        </div>
    </div>

    <div class="remote-alioss" {if empty($remote['type']) || $remote['type'] != '2'} style="display:none;" {/if}>
        <div class="alert alert-info">
            启用阿里oss后，请把/attachment目录（不包括此目录）下的子文件及子目录上传至阿里云oss, 相关工具：<br>
            <ul class="link-list">
            <li><a target="_blank" href="http://market.aliyun.com/products/52738003/cmgj000304.html?spm=5176.383663.9.21.faitxp" class="product-grey-font" data-spm-anchor-id="5176.383663.9.21">cloudfs4oss(ECS挂载文件盘工具)</a></li>
            <li><a target="_blank" href="http://market.aliyun.com/products/53690006/cmgj000281.html?spm=5176.383663.9.22.faitxp" class="product-grey-font" data-spm-anchor-id="5176.383663.9.22">官方推荐OSS客户端工具（Windows版）</a></li>
            <li><a target="_blank" href="http://market.aliyun.com/products/53690006/cmgj000282.html?spm=5176.383663.9.23.faitxp" class="product-grey-font" data-spm-anchor-id="5176.383663.9.23">官方推荐OSS客户端工具（Mac版）</a></li>
            <li><a target="_blank" href="http://market.aliyun.com/products/53690006/cmgj000208.html?spm=5176.383663.9.24.faitxp" class="product-grey-font" data-spm-anchor-id="5176.383663.9.24">Ftp4ossServer（OSS的FTP云工具）</a></li>
            <li><a target="_blank" href="http://bbs.aliyun.com/read/239565.html?spm=5176.383663.9.25.faitxp&amp;pos=2" class="product-grey-font" data-spm-anchor-id="5176.383663.9.25">OSS图片服务Demo工具</a></li>
            <li><a target="_blank" href="http://docs.aliyun.com/?spm=5176.383663.9.26.faitxp#/pub/oss/utilities/osscmd&amp;install" class="product-grey-font" data-spm-anchor-id="5176.383663.9.26">批量上传工具(Python)版</a></li>
            <li><a target="_blank" href="https://docs.aliyun.com/?spm=5176.383663.9.27.faitxp#/pub/oss/utilities/oss-import&amp;index" class="product-grey-font" data-spm-anchor-id="5176.383663.9.27">OSS数据迁移工具-OSS Import</a></li>
            <li><a target="_blank" href="http://market.aliyun.com/products/52738004/cmfw000394.html?spm=5176.383663.9.28.faitxp" class="product-grey-font" data-spm-anchor-id="5176.383663.9.28">海量数据迁移至OSS服务</a></li>
            <li><a target="_blank" href="http://bbs.aliyun.com/read/247023.html?spm=5176.383663.9.29.faitxp" class="product-grey-font" data-spm-anchor-id="5176.383663.9.29">更多官方推荐工具</a></li>
            </ul>

        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">Access Key ID</label>
            <div class="col-sm-9">
                <input type="text" name="alioss_key" class="form-control" value="{$remote['alioss_key']}" placeholder="" />
                <span class="help-block">
                    Access Key ID是您访问阿里云API的密钥，具有该账户完全的权限，请您妥善保管。
                </span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">Access Key Secret</label>
            <div class="col-sm-9">
                <input type="text" name="alioss_secret" class="form-control " value="{$remote['alioss_secret']}" placeholder="" />
                <span class="help-block">
                    Access Key Secret是您访问阿里云API的密钥，具有该账户完全的权限，请您妥善保管。(填写完Access Key ID 和 Access Key Secret 后请选择bucket)
                </span>
            </div>
        </div>
        <div class="form-group" id="bucket" {if empty($remote['alioss_key'])}style="display: none;{/if}">
            <label class="col-sm-2 control-label">Bucket选择</label>
            <div class="col-sm-9">
                <select name="alioss_bucket" class="form-control">
                </select>
                <span class="help-block">
                    完善Access Key ID和Access Key Secret资料后可以选择存在的Bucket(请保证bucket为可公共读取的)，否则请手动输入。
                </span>
            </div>
        </div>
        <div class="form-group">
         <label class="col-sm-2 control-label">自定义URL</label>
            <div class="col-sm-9">
                <input type="text" name="alioss_url" class="form-control" value="{$remote['alioss_url']}"/>
                    <span class="help-block">
                        阿里云oss支持用户自定义访问域名，如果自定义了URL则用自定义的URL，如果未自定义，则用系统生成出来的URL。注：自定义url开头加http://或https://结尾不加 ‘/’例：http://abc.com
                    </span>
            </div>
        </div>
<!--         <div class="form-group">
            <div class="">
                <button name="submit" class="btn btn-primary" value="submit">保存配置</button>
                <input type="hidden" name="token" value="{$_W['token']}" />
            </div>
        </div> -->
    </div>
    <div class="remote-qiniu" {if empty($remote['type']) || $remote['type'] != '3'} style="display:none;" {/if}>
    <div class="alert alert-info">
        启用七牛云存储后，请把/attachment目录（不包括此目录）下的子文件及子目录上传至七牛云存储, 相关工具：<br>
        <ul class="link-list">
            <li><a target="_blank" href="https://portal.qiniu.com/signin" class="product-grey-font" >七牛云存储</a></li>
        </ul>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">Accesskey</label>
        <div class="col-sm-9">
            <input type="text" name="qiniu_accesskey" class="form-control" value="{$remote['qiniu_accesskey']}" placeholder="" />
            <span class="help-block">用于签名的公钥</span>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">Secretkey</label>
        <div class="col-sm-9">
            <input type="text" name="qiniu_secretkey" class="form-control" value="{$remote['qiniu_secretkey']}" placeholder="" />
            <span class="help-block">用于签名的私钥</span>
        </div>
    </div>
    <div class="form-group" id="qiniubucket">
        <label class="col-sm-2 control-label">Bucket</label>
        <div class="col-sm-9">
            <input type="text" name="qiniu_bucket" class="form-control" value="{$remote['qiniu_bucket']}" placeholder="" />
            <span class="help-block">请保证bucket为可公共读取的</span>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">Url</label>
        <div class="col-sm-9">
            <input type="text" name="qiniu_url" class="form-control" value="{$remote['qiniu_url']}" placeholder="" />
            <span class="help-block">七牛支持用户自定义访问域名。注：url开头加http://或https://结尾不加 ‘/’例：http://abc.com</span>
        </div>
    </div>
<!--     <div class="form-group">
        <div class="">
            <button name="submit" class="btn btn-primary" value="submit">保存配置</button>
            <input type="hidden" name="token" value="{$_W['token']}" />
        </div>
    </div> -->
    </div>
    <div class="form-group">
        <div class="">
            <button name="submit" class="btn btn-primary" value="submit">保存配置</button>
            <input type="hidden" name="token" value="{$_W['token']}" />
        </div>
    </div>
</form>

<script type="text/javascript">
    $(function() {
        $('.encrypt').val(function() {
            return util.encrypt($(this).val());
        });
    });
    $('.js-checkremoteoss').on('click', function(){
        var bucket = $.trim($('select[name="alioss_bucket"]').val());
        if (bucket == '') {
            bucket = $.trim($(':text[name="alioss_bucket"]').val());
        }
        var param = {
            'key' : $.trim($(':text[name="alioss_key"]').val()),
            'secret' : $.trim($(':text[name="alioss_secret"]').val()),
            'url'  : $.trim($(':text[name="alioss_url"]').val()),
            'bucket' : bucket
        };
        $.post("{php echo url('system/attachment/oss')}",param, function(data) {
            var data = $.parseJSON(data);
            if(data.message.errno == 0) {
                util.message('配置成功');
                return false;
            }
            if(data.message.errno < 0) {
                util.message(data.message.message);
                return false;
            }
        });
    });
    $('.js-checkremoteqiniu').on('click', function(){
        var key = $.trim($(':text[name="qiniu_accesskey"]').val());
        if (key == '') {
            util.message('请填写Accesskey');
            return false;
        }
        var secret = $.trim($(':text[name="qiniu_secretkey"]').val());
        if (secret == '') {
            util.message('请填写Secretkey');
            return false;
        }
        var param = {
            'accesskey' : $.trim($(':text[name="qiniu_accesskey"]').val()),
            'secretkey' : $.trim($(':text[name="qiniu_secretkey"]').val()),
            'url'  : $.trim($(':text[name="qiniu_url"]').val()),
            'bucket' :  $.trim($(':text[name="qiniu_bucket"]').val())
        };
        $.post("{php echo url('system/attachment/qiniu')}",param, function(data) {
            var data = $.parseJSON(data);
            if(data.message.errno == 0) {
                util.message('配置成功');
                return false;
            }
            if(data.message.errno < 0) {
                util.message(data.message.message);
                return false;
            }
        });
    });
    var alibucket = '{$_W['setting']['remote']['alioss']['bucket']}';
    var buck =  function() {
        var key = $(':text[name="alioss_key"]').val();
        var secret = $(':text[name="alioss_secret"]').val();
        console.log(secret);
        if (secret.indexOf('*') >= 0) {
            secret = '{$_W['setting']['remote']['alioss']['secret']}';
        }
        if (key == '' || secret == '') {
            $('#bucket').hide();
            return false;
        }
        $.post("{php echo url('system/attachment/buckets')}", {'key' : key, 'secret' : secret}, function(data) {
            try {
                var data = $.parseJSON(data);
            } catch (error) {
                util.message('Access Key ID 或 Access Key Secret 填写错误，请重新填写。', '', 'error');
                $('#bucket').hide();
                $('select[name="alioss_bucket"]').val('');
                return false;
            }

            if (data.message.errno < 0 ) {
                return false;
            } else {
                $('#bucket').show();
                var bucket = $('select[name="alioss_bucket"]');
                bucket.empty();
                var buckets = eval(data.message.message);
                for (var i in buckets) {
                    var selected = alibucket == buckets[i]['name'] || alibucket ==  buckets[i]['name'] + '@@' + buckets[i]['location'] ? 'selected' : '';
                    bucket.append('<option value="' + buckets[i]['name'] + '@@' + buckets[i]['location'] + '"' + selected + '>'+buckets[i]['loca_name'] + '</option>');
                }
            }
        });
    };
    buck();
    $(':text[name="alioss_secret"]').blur(function() {buck();});
    $('form').submit(function() {
        if ($('[name="type"]:checked').val() == 2 && ($('select[name="alioss_bucket"]').val() == null || $('select[name="alioss_bucket"]').val() == '')) {
            util.message('请完善信息后再保存设置！');
            return false;
        }
    });
</script>
{/if}
